import { defineStore } from 'pinia';
import type { FeatureDataInterface } from "@/class/map/MapData";
import { reqGetMapdataByPage, reqGetMapDataTop } from '@/api/mapdata';
import { getMapDateByType } from '@/utils/map';
export const useMapInDatabase = defineStore('mapInDatabase', {
    state: () => ({
        elementInPage: [] as FeatureDataInterface[],
        elementInTop: [] as FeatureDataInterface[],
    }),
    actions: {
        pushElementToPage(item: FeatureDataInterface) {
            this.elementInPage.push(item);
        },
        pushElementToTop(item: FeatureDataInterface) {
            this.elementInTop.push(item);
        },
        clearElementInPage(element: FeatureDataInterface) {
            const index = (this.elementInPage as FeatureDataInterface[]).findIndex(item => item.featureData.id === element.featureData.id);
            this.elementInPage.splice(index, 1);
        },
        clearAllElementInPage() {
            this.elementInPage.splice(0, this.elementInPage.length);
        },
        clearAllElementInTop() {
            this.elementInTop.splice(0, this.elementInTop.length);
        },
        addDataToPage(features: FeatureDataInterface[]) {
            features.forEach(feature => {
                this.pushElementToPage(feature);
            });
        },
        addDataToTop(features: FeatureDataInterface[]) {
            features.forEach(feature => {
                this.pushElementToTop(feature);
            });
        },
        async getElementInPage(currentPage: number, pageSize: number) {
            
            const mapData = await reqGetMapdataByPage(currentPage, pageSize)
            const dataList = mapData.data.map(getMapDateByType)
            this.clearAllElementInPage()
            this.addDataToPage(dataList)
        },
        async getElementInTop() {
            const mapData = await reqGetMapDataTop()
            let dataList = mapData.data.map(getMapDateByType)
            this.clearAllElementInTop()
            this.addDataToTop(dataList)
        }
    },
    getters: {}
});
